package com.souku.finance.application.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;

import com.souku.finance.application.R;
import com.souku.finance.application.utils.ToastUtil;

/**
 * Created by Android on 2016/12/13.
 */

public abstract class BaseFragment extends Fragment {


    public final int REQUEST = 1;
    public final int DEFAULT = 2;
    private boolean isRegister = true;

    public Handler mhandle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case REQUEST:
                    IntentFilter mFilter = new IntentFilter();
                    mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
                    getActivity().registerReceiver(myNetReceiver, mFilter);
                    break;
                case DEFAULT:
                    mhandleCallback();
                    break;
            }
        }
    };

    public void mhandleCallback() {

    }

    @Override
    public void onResume() {
        super.onResume();
        showFragment();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            showFragment();
        }
    }

    private View content;

    public void setContent(View content) {
        this.content = content;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mhandle.sendEmptyMessage(REQUEST);
    }

    private ConnectivityManager mConnectivityManager;
    private NetworkInfo netInfo;
    private BroadcastReceiver myNetReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                mConnectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
                netInfo = mConnectivityManager.getActiveNetworkInfo();
                if (netInfo != null && netInfo.isAvailable()) {
                    if (!isRegister) {
                        internet();
                    }
                    View tipView = content.findViewById(R.id.tipview);
                    if (tipView != null) {
                        tipView.setVisibility(View.GONE);
                    }
                } else {
                    View tipView = content.findViewById(R.id.tipview);
                    if (tipView != null) {
                        tipView.setVisibility(View.VISIBLE);
                    } else {
                        ToastUtil.show("网络连接错误");
                    }
                }
                isRegister = false;
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (myNetReceiver != null) {
            getActivity().unregisterReceiver(myNetReceiver);
        }
        mhandle.removeCallbacksAndMessages(null);
    }

    public abstract void internet();

    public abstract void showFragment();
}
